package org.jeecg.modules.crawler.job;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.modules.crawler.entity.NewCrawlerInfo;
import org.jeecg.modules.crawler.service.INewCrawlerInfoService;
import org.jeecg.modules.crawler.service.INewCrawlerKeywordService;
import org.jeecg.modules.wechat.entity.SeuWx;
import org.jeecg.modules.wechat.service.ISeuWxService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 任务定时发送
 */
@Slf4j
public class SeuWXCrawlerJob implements Job {

	@Autowired
	private ISeuWxService seuWxService;



	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		try {
			HttpClient httpClient = HttpClientBuilder.create().build();
			HttpPost httpPost = new HttpPost("https://api.newrank.cn/api/sync/weixin/account/articles");

			// 设置请求头
			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			httpPost.setHeader("key", "d62e6b3be57f419b8f771ab35");

			// 创建参数列表
			List<NameValuePair> params = new ArrayList<>();
			params.add(new BasicNameValuePair("account", "seu_dndx"));
			params.add(new BasicNameValuePair("to", DateTimeUtil.getTodayChar8En()+" 00:00:00"));
			params.add(new BasicNameValuePair("from", DateTimeUtil.getLast30DayChar8En()+" 23:59:59"));
			params.add(new BasicNameValuePair("page", "1"));
			params.add(new BasicNameValuePair("size", "50"));

			// 设置请求体
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

			// 发送请求并获取响应
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();

			// 处理响应内容
			if (entity != null) {
				String responseString = EntityUtils.toString(entity, "UTF-8");
				System.out.println(responseString);
				ObjectMapper mapper = new ObjectMapper();
				Map<String, Object> responseMap = mapper.readValue(responseString, Map.class);
				List<Map<String,Object>> listMap = (List<Map<String,Object>>)responseMap.get("data");
				List<SeuWx> wxList = new ArrayList<>();
				listMap.stream().forEach(item ->{
					System.out.println(item.get("title"));
					SeuWx wx = new SeuWx();
					wx.setTitle(item.get("title").toString());
					wx.setAuthor(item.get("author").toString());
					wx.setImg(item.get("imageUrl").toString());
					wx.setCommentnum(item.get("commentNum").toString());
					wx.setLikenum(item.get("likeNum").toString());
					wx.setPrelike(item.get("preLike").toString());
					wx.setReadnum(item.get("readNum").toString());
					wx.setPublictime(item.get("publicTime").toString());
//					wxList.add(wx);
					try {
						seuWxService.saveOrUpdate(wx);
					}catch (Exception e){
						e.printStackTrace();
					}
				});
//				System.out.println(responseMap); // 打印转换后的 Map
//				seuWxService.saveOrUpdateBatch(wxList);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}

//
//	public static void main(String[] args) {
//		try {
//			HttpClient httpClient = HttpClientBuilder.create().build();
//			HttpPost httpPost = new HttpPost("https://api.newrank.cn/api/sync/weixin/account/articles");
//
//			// 设置请求头
//			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//			httpPost.setHeader("key", "b96bb48a635342a8b571264db");
//
//			// 创建参数列表
//			List<NameValuePair> params = new ArrayList<>();
//			params.add(new BasicNameValuePair("account", "seu_dndx"));
//			params.add(new BasicNameValuePair("to", DateTimeUtil.getTodayChar8En()+" 00:00:00"));
//			params.add(new BasicNameValuePair("from", DateTimeUtil.getLast30DayChar8En()+" 23:59:59"));
//			params.add(new BasicNameValuePair("page", "1"));
//			params.add(new BasicNameValuePair("size", "50"));
//
//			// 设置请求体
//			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//
//			// 发送请求并获取响应
//			HttpResponse response = httpClient.execute(httpPost);
//			HttpEntity entity = response.getEntity();
//
//			// 处理响应内容
//			if (entity != null) {
//				String responseString = EntityUtils.toString(entity, "UTF-8");
//				System.out.println(responseString);
//				ObjectMapper mapper = new ObjectMapper();
//				Map<String, Object> responseMap = mapper.readValue(responseString, Map.class);
//				List<Map<String,Object>> listMap = (List<Map<String,Object>>)responseMap.get("data");
//				listMap.stream().forEach(item ->{
//					System.out.println(item.get("title"));
//				});
////				System.out.println(responseMap); // 打印转换后的 Map
//			}
//		}catch (Exception e){
//			e.printStackTrace();
//		}
//	}


}
